package local.blueheart.data.springdatademo.service.impl;

import local.blueheart.data.springdatademo.annotation.TaskWorker;
import local.blueheart.data.springdatademo.domain.task.TaskWorkerVO;
import local.blueheart.data.springdatademo.service.TaskWorkerService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;

import java.lang.annotation.Annotation;
import java.util.ArrayList;
import java.util.List;

@Component
public class TaskWorkerServiceImpl implements TaskWorkerService {

    private static final Logger LOGGER = LoggerFactory.getLogger(TaskWorkerServiceImpl.class);

    @Autowired
    private ApplicationContext applicationContext;

    @Override
    public List<TaskWorkerVO> getTaskWorkerList() {
        String[] beanNames = applicationContext.getBeanNamesForAnnotation(TaskWorker.class);
        List<TaskWorkerVO> results =  new ArrayList<>();
        for (String beanName : beanNames) {
            Object o = applicationContext.getBean(beanName);
            Annotation[] declaredAnnotations = o.getClass().getDeclaredAnnotations();
            for (Annotation annotation : declaredAnnotations) {
                //TaskWorker workAnno = (TaskWorker) annotation;
                LOGGER.debug(((TaskWorker) annotation).value() + "," + o.getClass().getName());
                TaskWorkerVO vo = new TaskWorkerVO(((TaskWorker) annotation).value(),o.getClass().getName());
                results.add(vo);
            }
        }
        return results;
    }

}
